定义:
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的类。
类图:
类型:创建型
实例:
提供对不同的数据库访问的支持
C++ 实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include <iostream> #include <string> using namespace std;
class IUser { public: virtual void getUser()=0; virtual void setUser()=0; };
class SqlUser:public IUser { public: void getUser() { cout<<"在sql中返回user"<<endl; } void setUser() { cout<<"在sql中设置user"<<endl; } };
class AccessUser:public IUser { public: void getUser() { cout<<"在Access中返回user"<<endl; } void setUser() { cout<<"在Access中设置user"<<endl; } };
class IDepartment { public: virtual void getDepartment()=0; virtual void setDepartment()=0; };
class SqlDepartment:public IDepartment { public: void getDepartment() { cout<<"在sql中返回Department"<<endl; } void setDepartment() { cout<<"在sql中设置Department"<<endl; } };
class AccessDepartment:public IDepartment { public: void getDepartment() { cout<<"在Access中返回Department"<<endl; } void setDepartment() { cout<<"在Access中设置Department"<<endl; } };
class IFactory { public: virtual IUser *createUser()=0; virtual IDepartment *createDepartment()=0; };
class SqlFactory:public IFactory { public: IUser *createUser() { return new SqlUser(); } IDepartment *createDepartment() { return new SqlDepartment(); } };
class AccessFactory:public IFactory { public: IUser *createUser() { return new AccessUser(); } IDepartment *createDepartment() { return new AccessDepartment(); } };
int main() { IFactory *factory=new SqlFactory(); IUser *user = user=factory->createUser(); IDepartment * department = factory->createDepartment(); user->getUser(); user->setUser(); department->getDepartment(); department->setDepartment();
return 0; }
|
Python 实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
class SqlUser: def getUser(self): print "在sql中返回user" def setUser(self): print "在sql中设置user"
class AccessUser: def getUser(self): print "在Access中返回user" def setUser(self): print "在Access中设置user"
class SqlDepartment: def getDepartment(self): print "在sql中返回Department" def setDepartment(self): print "在sql中设置Department"
class AccessDepartment: def getDepartment(self): print "在Access中返回Department" def setDepartment(self): print "在Access中设置Department"
class SqlFactory: def createUser(self): return SqlUser() def createDepartment(self): return SqlDepartment()
class AccessFactory: def createUser(self): return AccessUser() def createDepartment(self): return AccessDepartment()
if __name__ == '__main__': factory = SqlFactory() user = factory.createUser() department = factory.createDepartment()
user.getUser() user.setUser() department.getDepartment() department.setDepartment()
|
Tips:
Python
实现的抽象工厂模式相比与C++
来说并没有什么显著特点。抽象工厂模式
可以结合简单工厂
加反射
写出更简洁、灵活的客户端代码,有兴趣的同学自己尝试一下。
Python
的抽象工厂模式实现比C++
版本简洁了不少,这得益于Python
的鸭子类型
。有关鸭子类型
详见:《当DP遇见Py(五) — 工厂方法模式》。